package com.ysw.drivers.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author ysw
 */
public class DBContextHolder {

    private static final Logger LOG = LoggerFactory.getLogger(DBContextHolder.class);

    //对当前线程的操作-线程安全的
    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();

    //调用此方法,切换数据源
    public static void setDataSource(String dataSource) {
        contextHolder.set(dataSource);
        LOG.info("己切换到数据源:{} ", dataSource);
    }

    //获取数据源
    public static String getDataSource() {
        return contextHolder.get();
    }

    //删除数据源
    public static void clearDataSource() {
        contextHolder.remove();
        LOG.info("己切换到主数据源");
    }
}
